﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Factory.cs" company="SmartPlan">
//   SmartPlan 2011
// </copyright>
// <summary>
//   Defines the Factory type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------


namespace DataSetCreator
{
    using System;
    using DbDiff.Common.DataAccess;
    using DbDiff.Common.Helper;

    /// <summary>
    /// The factory class for help check/build strogly typed datasets
    /// </summary>
    public static class Factory
    {
        /// <summary>
        /// Check the error messages
        /// </summary>
        public static string CheckErrorMessages()
        {
            string errorMessage = string.Empty;

            var errorMessageTables = new ErrorMessageTables();
            var databaseObjectDs = new DbObjectsDs();

            // CHECK db
            foreach (string s in Enum.GetNames(typeof(DatabaseObjectTypes)))
            {
                if (
                    !(s != DatabaseObjectTypes.ReplicationProcedure.ToString() &
                      s != DatabaseObjectTypes.Details.ToString()
                      & s != DatabaseObjectTypes.User.ToString()
                      & s != DatabaseObjectTypes.Property.ToString()
                      & s != DatabaseObjectTypes.Diff.ToString()
                      & s != DatabaseObjectTypes.DiffDetails.ToString()))
                {
                    continue;
                }

                int index = errorMessageTables.FindIndexByName(s);
                for (int i = 0; i < databaseObjectDs.Tables[s].Columns.Count; i++)
                {
                    if (!errorMessageTables.Tables[index].Columns.Contains(databaseObjectDs.Tables[s].Columns[i].ColumnName))
                    {
                        errorMessage += string.Format("Please define errormessage for {0},{1} !!!", s, databaseObjectDs.Tables[s].Columns[i].ColumnName);
                    }
                    else 
                    {
                        // else dtObjects.Tables[s].Columns[i].Caption = dm.Tables[s].Columns[dtObjects.Tables[s].Columns[i].ColumnName].Caption;
                        if (errorMessageTables.Tables[index].Columns[databaseObjectDs.Tables[s].Columns[i].ColumnName].Caption == errorMessageTables.Tables[index].Columns[databaseObjectDs.Tables[s].Columns[i].ColumnName].ColumnName)
                        {
                            // dm.Tables[s].Columns[dtObjects.Tables[s].Columns[i].ColumnName].Caption = "false";
                            errorMessage = string.Format("this.Tables[dbObjectsTable.{0}.ToString()].Columns[\"{1}\"].Caption = {2};", s, databaseObjectDs.Tables[s].Columns[i].ColumnName, "\"false\"");
                        }
                    }
                }

                for (int i = 0; i < errorMessageTables.Tables[index].Columns.Count; i++)
                {
                    if (!databaseObjectDs.Tables[s].Columns.Contains(errorMessageTables.Tables[index].Columns[i].ColumnName))
                    {
                        errorMessage = string.Format("Please remove from errormessages {0},{1}", s, errorMessageTables.Tables[index].Columns[i].ColumnName);
                    }
                }
            }

            return errorMessage;
        }

    }
}
